package cn.tx;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class MainMenu {

    //创建一个集合来储存
    static List<Bills> billList = new ArrayList<>();//共享数据

    //类加载的时候会第一时间执行，在billsList里面加入初始化的数据
    static {
        billList.add(new Bills("吃饭支出",247,"交行","支出","2021-03-02","家庭聚餐"));
        billList.add(new Bills("吃饭支出",247,"交行","支出","2021-03-02","家庭聚餐"));
        billList.add(new Bills("工资收入",12345,"现金","收入","2021-03-15","开工资了"));
        billList.add(new Bills("服装支出",1998,"现金","支出","2021-04-02","买衣服"));
        billList.add(new Bills("吃饭支出",325,"现金","支出","2021-06-18","朋友聚餐"));
        billList.add(new Bills("股票收入",8000,"工行","收入","2021-07-28","股票大涨"));
        billList.add(new Bills("股票收入",5000,"工行","收入","2021-08-28","股票又大涨"));
        billList.add(new Bills("工资收入",5000,"交行","收入","2021-09-28","又开工资了"));
        billList.add(new Bills("礼金支出",5000,"现金","支出","2021-11-28","朋友结婚"));
        billList.add(new Bills("其他支出",1560,"现金","支出","2021-12-29","丢钱了"));
        billList.add(new Bills("交通支出",2300,"交行","支出","2020-10-29","油价还在涨啊"));
        billList.add(new Bills("吃饭支出",1000,"工行","支出","2020-09-29","又开饭"));
        billList.add(new Bills("工资收入",1000,"现金","收入","2020-11-30","开资"));
        billList.add(new Bills("交通支出",2000,"现金","支出","2020-07-30","机票好贵"));
        billList.add(new Bills("工资收入",5000,"现金","收入","2020-05-30","又开资"));
    }

    public static void main(String[] args) {
        run();
    }

    public static void showMain(){
        System.out.println("---------------------随手记----------------------");
        System.out.println("1.添加账务  2.删除账务  3.查询账务  4.退出系统");
        System.out.println("请输入功能序号【1-4】：");
    }

    public static void run(){
        showMain();

        //创建一个flag标志
        boolean flag = true;
        // 创建扫描器
        Scanner scanner = new Scanner(System.in);
        while (flag) {
            //获得输入的选项
            int op = scanner.nextInt();
            //判断1,2,3,4
            switch (op) {
                case 1:
                    addBills();
                    break;
                case 2:
                    delBills();
                    break;
                case 3:
                    select();
                    break;
                case 4:
                    flag = false;
                    break;
                default:
                    System.out.println("请重新输入：");
            }
        }
        System.out.println("退出系统，拜拜");
    }

    private static void delBills() {
        System.out.println("随手记>>>账务删除");
        Scanner scanner = new Scanner(System.in);
        int id = scanner.nextInt();
        billList.remove(id - 1);
        System.out.println("删除成功");
        showMain();
    }

    private static void addBills() {
        System.out.println("随手记>>>账务添加");
        Scanner scanner = new Scanner(System.in);
        Bills bills = new Bills();

        System.out.println("请输入类型：");
        bills.setName(scanner.next());
        System.out.println("请输入账户：");
        bills.setAccount(scanner.next());
        System.out.println("请输入收支类型：");
        bills.setType(scanner.next());
        System.out.println("请输入金额：");
        bills.setTotal(scanner.nextDouble());
        System.out.println("请输入时间：");
        bills.setTime(scanner.next());
        System.out.println("请输入备注：");
        bills.setDesc(scanner.next());

        billList.add(bills);
        System.out.println("添加账务成功！");

        showMain();
    }

    /**
     * 三种查询  1.查询所有  2.按时间区间查询  3.收入和支出的类型查询
     */
    private static void select() {
        System.out.println("随手记>>>账务查询");
        System.out.println("1.查询所有  2.按时间区间查询  3.收入和支出的类型查询");
        //创建扫描器
        Scanner scanner = new Scanner(System.in);
        int op = scanner.nextInt();
        switch (op){
            case 1:
                selectAll();
                break;
            case 2:
                selectByDate();
                break;
            case 3:
                selectByType();
                break;
        }
        //展示上一级的主菜单
        showMain();
    }

    private static void selectByType() {
        System.out.println("随手记>>>账务查询>>>按时间区间查询");
        System.out.println("请输入（收支类型）：");
        //创建扫描器
        Scanner scanner = new Scanner(System.in);
        // 收入，支出
        String type = scanner.next();

        //筛选一下集合中type是支出的元素，集合中的查询 l.8之后集合可根据自己的想法做筛选
        //集合stream流
        List<Bills> billsList = billList.stream().filter(bills -> {
            String type1 = bills.getType();
            return type1.equals(type);
        }).collect(Collectors.toList());
        print(billsList);

    }

    private static void selectByDate() {
        //创建一个时间格式化的对象
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        System.out.println("随手记>>>账务查询>>>收入和支出的类型");
        //创建扫描器
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入开始时间：");
        String start = scanner.next();
        System.out.println("请输入结束时间：");
        String end = scanner.next();
        List<Bills> billsList = billList.stream().filter(bills -> {
            String time = bills.getTime();
            //把字符串解析成具体的时间
            try {
                Date startDate = format.parse(start);
                Date endDate = format.parse(end);
                Date timeDate = format.parse(time);
                if (timeDate.before(endDate) && timeDate.after(startDate)) {
                    return true;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return false;
        }).collect(Collectors.toList());
        print(billsList);
    }

    private static void selectAll() {
        print(billList);
    }

    public static void print(List<Bills> billsList){
        System.out.println("ID\t\t类型\t\t\t账户\t\t类型\t\t金额\t\t\t时间\t\t\t\t备注");
        for (int i = 0; i < billsList.size(); i++) {
            Bills bills = billsList.get(i);
            System.out.println(i + 1 + "\t\t"+ bills.getName() + "\t\t" + bills.getTotal() + "\t\t"
             + bills.getType() + "\t\t" + bills.getAccount() + "\t\t\t" + bills.getTime() + "\t\t\t"
             + bills.getDesc());
        }
    }
}
